import {Component, OnInit, ViewChild, ViewContainerRef} from '@angular/core';
import {CoffeIncoming} from '../../../../model/CoffeIncoming';
import {NzModalService} from 'ng-zorro-antd/modal';
import {CoffeeincomeditComponent} from '../coffeeincomedit/coffeeincomedit.component';
import * as uuid from 'uuid';
import {AdminService} from "../../../../services/admin.service";
import {AlertService} from "../../../../services/alert.service";
import {NzUploadChangeParam} from "ng-zorro-antd/upload";
import {Rawbeaninventory} from '../../../../model/inventory/Rawbeaninventory';

@Component({
  selector: 'app-coffeeincomlist',
  templateUrl: './coffeeincomlist.component.html',
  styleUrls: ['./coffeeincomlist.component.css']
})
export class CoffeeincomlistComponent implements OnInit {
  il: Array<CoffeIncoming>;
  @ViewChild('basicTable') basicTable: any;
  sl: Array<Rawbeaninventory>;
  total = 0;


  constructor(private modal: NzModalService, private viewContainerRef: ViewContainerRef,
              public adminService: AdminService, private alertService: AlertService) {
    this.il = new Array<CoffeIncoming>();
    this.sl = new Array<Rawbeaninventory>();
  }

  ngOnInit(): void {
    this.getCoffeeincomings()
    this.loadRawBeanInventoryStastics();
  }

  getCoffeeincomings() {
    this.adminService.getCoffeeincomings().then((data: any) => {
      if (data.msg == "ok") {
        this.il = data.list
        this.loadRawBeanInventoryStastics();
      } else {
        this.alertService.showError("获取咖啡生豆入库失败")
      }
    })
  }

  income() {

    const modalwindow = this.modal.create({
      nzTitle: '咖啡生豆入库信息编辑',
      nzContent: CoffeeincomeditComponent,
      nzViewContainerRef: this.viewContainerRef,
      nzComponentParams: {
        optype: "add",

      },

      nzOnOk: () => new Promise(resolve => {
        let temp = modalwindow.getContentComponent().c;
        if (!temp.jhrq) {
          this.alertService.showWarning('进货日期必填');
          resolve(false)
        }else if(!temp.incompc){
          this.alertService.showWarning('进货批次必填 ');
          resolve(false)
        }else if(!temp.pm){
          this.alertService.showWarning('品名必须选择');
          resolve(false)
        }else if(!temp.cpxx){
          this.alertService.showWarning('产品信息必须填写 ');
          resolve(false)
        }else if(!temp.rksl){
          this.alertService.showWarning('入库数量必须填写 ');
          resolve(false)
        }else if(!temp.gys){
          this.alertService.showWarning('供应商必须选择 ');
          resolve(false)
        }else if(!temp.cgry){
          this.alertService.showWarning('采购人员必须选择 ');
          resolve(false)
        }else {
          modalwindow.getContentComponent().save()
          setTimeout(() => {
            resolve(true);
            this.getCoffeeincomings();
          }, 1000)
        }
      })
    })

  }

  update(id: string) {
    const modalwindow = this.modal.create({
      nzTitle: '咖啡生豆入库信息编辑',
      nzContent: CoffeeincomeditComponent,
      nzViewContainerRef: this.viewContainerRef,
      nzComponentParams: {
        optype: "update",
        id: id
      },

      nzOnOk: () => new Promise(resolve => {
        // modalwindow.getContentComponent().save();

        let temp = modalwindow.getContentComponent().c;
        if (!temp.jhrq) {
          this.alertService.showWarning('进货日期必填');
          resolve(false)
        }else if(!temp.incompc){
          this.alertService.showWarning('进货批次必填 ');
          resolve(false)
        }else if(!temp.pm){
          this.alertService.showWarning('品名必须选择');
          resolve(false)
        }else if(!temp.cpxx){
          this.alertService.showWarning('产品信息必须填写 ');
          resolve(false)
        }else if(!temp.rksl){
          this.alertService.showWarning('入库数量必须填写 ');
          resolve(false)
        }else if(!temp.gys){
          this.alertService.showWarning('供应商必须选择 ');
          resolve(false)
        }else if(!temp.cgry){
          this.alertService.showWarning('采购人员必须选择 ');
          resolve(false)
        } else {
          modalwindow.getContentComponent().save()
          setTimeout(() => {
            resolve(true);
            this.getCoffeeincomings();
            this.loadRawBeanInventoryStastics();
          }, 1000)
        }
      })
    })
  }

  delete(id: string) {
    this.adminService.deleteCoffeeincomingByid(id).then((data: any) => {
      if (data.msg == "ok") {
        this.getCoffeeincomings()
        this.alertService.showSuccess("删除咖啡生豆入库信息成功")
      } else {
        this.alertService.showError("删除咖啡生豆入库信息失败")
      }
    })
  }


  loadRawBeanInventoryStastics() {
    this.adminService.getRawBeanNum()
      .then((data: any) => {
        if (data.msg == 'ok') {
          this.sl = new Array<Rawbeaninventory>();
          this.sl = data.list;
          //计算总数
          this.total = 0;
          for (let i = 0; i < data.list.length; i++) {
            this.total = this.total + data.list[i].total;
          }
        }
      })
  }


}
